<?php
	class AdminDthink_AreaController extends Main_Controller_Action
	{
		public function init()
		{
			$this->loadTemplates('admin');
			$db = Zend_Registry::get('db');
		}
		
		public function preDispatch()
		{
			$auth = Zend_Auth::getInstance();
			if((!$auth->hasIdentity()) || ($auth->getIdentity()->level != 0))
			{
				if($this->_request->getActionName() != 'login')
				{
					$this->_redirect('/admin-dthink/index/login');
				}
			}
		}
		
		public function indexAction()
		{
			$this->view->headTitle('Quản lý vùng miền');
			$area = new AdminDthink_Model_Area();
			
			if($_POST['add'])
			{
				$this->_redirect('admin-dthink/area/create');
			}
			if($_POST['refresh'])
			{
				$this->_redirect('admin-dthink/area');
			}
			if($_POST['delete'])
			{
				$listId = $this->_request->getParam('check');
				$area->deleteAreas($listId);
				$this->_redirect('admin-dthink/area');
			}
			
			$listArea = $area->listArea();
			$paginator = Zend_Paginator::factory($listArea);
			$paginator->setItemCountPerPage(20);
			$paginator->setPageRange(5);
			$currentPage = $this->_request->getParam('page', 1);
			$paginator->setCurrentPageNumber($currentPage);
			
			$this->view->total = count($listArea);
			$this->view->data = $paginator;
		}
		
		public function createAction()
		{
			$this->view->headTitle('Thêm vùng miền mới');
			$area = new AdminDthink_Model_Area();
			
			if($this->_request->isPost())
			{
                $filter = new Zend_Filter_StringTrim();
				$area_name = $filter->filter($this->_request->getParam('area_name'));
				$flag = true;
				$validation = new AdminDthink_Form_ValidateArea();
				if(!$validation->validAreaName($area_name))
				{
					$this->view->errorAreaName = 'Tên vùng miền không được để trống và quá 100 kí tự';
					$flag = false;
				}
				if($area->checkArea($area_name) > 0)
				{
					$this->view->errorAreaName = 'Tên vùng miền đã tồn tại';
					$flag = false;
				}
				
				if($flag == true)
				{	
					$result = $area->addArea($area_name);
					if($result)
					{
						$this->_redirect('admin-dthink/area');
					}
					else
					{
						$this->view->error = 'Có lỗi xảy ra';
					}
				}	
			}
		}
		
		public function editAction()
		{
			$this->view->headTitle('Sửa thông tin vùng miền');
			$area = new AdminDthink_Model_Area();
			$area_id = $this->_request->getParam('id');
			$getArea = $area->getArea($area_id);
			$this->view->area = $getArea;
			
			if($this->_request->isPost())
			{
                $filter = new Zend_Filter_StringTrim();
				$area_name = $filter->filter($this->_request->getParam('area_name'));
				$status	= $this->_request->getParam('status');
				$flag = true;
				$validation = new AdminDthink_Form_ValidateArea();
				if(!$validation->validAreaName($area_name))
                {
					$this->view->errorAreaName = 'Tên vùng miền không được để trống và quá 100 kí tự';
					$flag = false;
				}
                if($getArea['area_name'] != $area_name)
                {
                    if($area->checkArea($area_name, $area_id) > 0)
    				{
    					$this->view->errorAreaName = 'Tên vùng miền đã tồn tại';
    					$flag = false;
    				}
                }
				
				if($flag == true)
				{
					$result = $area->editArea($area_id, $area_name, $status);
					$this->_redirect('admin-dthink/area');
				}
			}
		}
		
		public function deleteAction()
		{
			$delete = new AdminDthink_Model_Area();
			$area_id = $this->_request->getParam('id');
			$result = $delete->deleteArea($area_id);
			if($result)
			{
				$this->_redirect('admin-dthink/area');
			}
			else
			{
				return false;
			}
		}
		
		public function searchAction()
		{
			$this->view->headTitle('Tìm kiếm vùng miền');
            $filter = new Zend_Filter_StringTrim();
			$keyword = $filter->filter($this->_request->getParam('keyword'));
			$this->view->searchValue = $keyword;
			$area = new AdminDthink_Model_Area();
			$flag = true;
				
			if($this->_request->isPost())
			{
				$validation = new AdminDthink_Form_ValidateArea();
				if($validation->validAreaSearch($keyword))
				{
					$this->view->searchValue = $keyword;
				}
				else
				{
					$this->view->errorSearch = 'Bạn chưa nhập từ khóa';
					$result = $area->searchArea($keyword);
					$paginator = Zend_Paginator::factory($result);
					$paginator->setItemCountPerPage(20);
					$paginator->setPageRange(5);
					$currentPage = $this->_request->getParam('page', 1);
					$paginator->setCurrentPageNumber($currentPage);
			
					$this->view->total = count($result);
					$this->view->data = $paginator;
					$flag = false;
				}
			
				if($flag == true)
				{
					$result = $area->searchArea($keyword);
					$paginator = Zend_Paginator::factory($result);
					$paginator->setItemCountPerPage(20);
					$paginator->setPageRange(5);
					$currentPage = $this->_request->getParam('page', 1);
					$paginator->setCurrentPageNumber($currentPage);
			
					$this->view->total = count($result);
					$this->view->data = $paginator;
				}
			}
				
			if($_POST['add'])
			{
				$this->_redirect('admin-dthink/area/create');
			}
			if($_POST['refresh'])
			{
				$this->_redirect('admin-dthink/area');
			}
			if($_POST['delete'])
			{
				$listId = $this->_request->getParam('check');
				$result = $area->deleteAreas($listId);
				$this->_redirect('admin-dthink/area');
			}
			
			$validation = new AdminDthink_Form_ValidateArea();
			if($validation->validAreaSearch($keyword))
			{
				$this->view->searchValue = $keyword;
			}
			else
			{
				$this->view->errorSearch = 'Bạn chưa nhập từ khóa';
				$result = $area->searchArea($keyword);
				$paginator = Zend_Paginator::factory($result);
				$paginator->setItemCountPerPage(10);
				$paginator->setPageRange(5);
				$currentPage = $this->_request->getParam('page', 1);
				$paginator->setCurrentPageNumber($currentPage);
					
				$this->view->total = count($result);
				$this->view->data = $paginator;
				$flag = false;
			}
			
			if($flag == true)
			{
				$result = $area->searchArea($keyword);
				$paginator = Zend_Paginator::factory($result);
				$paginator->setItemCountPerPage(20);
				$paginator->setPageRange(5);
				$currentPage = $this->_request->getParam('page', 1);
				$paginator->setCurrentPageNumber($currentPage);
				
				$this->view->total = count($result);
				$this->view->data = $paginator;
			}			
		}
	}
